﻿#include "Tracer.h"
#include "ComUtils.h"
using namespace libemb;

#include <iostream>

void TestUtils(void)
{
	std::string inputStr;
	while(1)
	{
		TRACE_YELLOW("q ---> quit.\n");
		TRACE_YELLOW("1 ---> endian test.\n");
		TRACE_YELLOW("2 ---> findString.\n");
		TRACE_YELLOW("3 ---> hexString2Bytes.\n");
		TRACE_YELLOW("4 ---> bytes2HexString.\n");
        TRACE_YELLOW("5 ---> utf8 & unicode.\n");
        TRACE_YELLOW("6 ---> cutString.\n");
        TRACE_YELLOW("7 ---> trimBeginWith.\n");
        TRACE_YELLOW("8 ---> splitString.\n");
        TRACE_YELLOW("9 ---> replaceString.\n");
        TRACE_YELLOW("10 ---> getPidsByName.\n");
		std::cin >> inputStr;
        std::cout << inputStr<<std::endl;
		if("q"==inputStr) 
		{
			return;
		}
		else if("1"==inputStr)
		{
			uint16 tmp16,x16=0xABCD;
			uint32 tmp32,x32=0x12345678;

			if(ComUtils::isBigEndian())
			{
				TRACE_DBG("Host is BigEndian.\n");
			}
			else
			{
				TRACE_DBG("Host is LittleEndian.\n");
			}

			tmp16 = ComUtils::host2LitEndian(x16);
			TRACE_DBG("0xABCD change to LittleEndian(0x%04X).\n",tmp16);
			tmp32 = ComUtils::host2LitEndian(x32);
			TRACE_DBG("0x12345678 change to LittleEndian(0x%08x).\n",tmp32);
			
			tmp16 = ComUtils::host2BigEndian(x16);
			TRACE_DBG("0xABCD change to BigEndian(0x%04X).\n",tmp16);
			tmp32 = ComUtils::host2BigEndian(x32);
			TRACE_DBG("0x12345678 change to BigEndian(0x%08x).\n",tmp32);

            tmp16 = ComUtils::endianReverse(x16); 
			TRACE_DBG("0xABCD reverse Endian(0x%04X).\n",tmp16);
			tmp32 = ComUtils::endianReverse(x32);
			TRACE_DBG("0x12345678 reverse Endian(0x%08x).\n",tmp32);

		}
		else if("2"==inputStr)
		{
			std::string result;
			std::string source="3A = \"AAA\"     \n  3B = BBB;";
			result=ComUtils::findString(source, "3A", "\"", "\"");
			std::cout<<"test 3A result="<<result<<std::endl;

			result=ComUtils::findString(source, "3B", "= ", ";");
			std::cout<<"test 3B result="<<result<<std::endl;

			//source="\" 123456  \"\"";
			source="  123456   ";
			source = ComUtils::trimEndingBlank(source);
			cout<<"trim result="<<source<<endl;
		}
		else if("3"==inputStr)
		{
			std::string str="1234abcd";
			char codebuf[4]={0};
			ComUtils::hexString2Bytes(str, codebuf, sizeof(codebuf));
			TRACE_DBG("\"%s\"==>:\n",str.c_str());
			TRACE_HEX("str2code",(char*)codebuf, sizeof(codebuf));
		}
		else if("4"==inputStr)
		{
			char codebuf[4]={0x56,0x78,0xab,0xcd};
			std::string str=ComUtils::bytes2HexString(codebuf, sizeof(codebuf));
			TRACE_HEX("code2str",(char*)codebuf, sizeof(codebuf));
			TRACE_DBG("codestr==>\"%s\"\n",str.c_str());
		}
        else if("5"==inputStr)
		{
			std::string yan=ComUtils::unicodeOneToUtf8String(0x4E25);
            TRACE_DBG("[0x4E25](unicode)===>[%s](utf8)\n",yan.c_str());
            uint32 unicode=ComUtils::utf8OneToUnicode(yan.c_str());
            TRACE_DBG("[%s](utf8)===>[0x%X](unicode)\n",yan.c_str(),unicode);
		}
        else if("6"==inputStr)
		{
			std::string source=":1,aaaaa,2,bbbbb,3,cccccc\r";
            std::vector<std::string> strVector=ComUtils::cutString(source, ":", "\r", ",");
            for(int i=0;i<strVector.size();i++)
            {
                TRACE_DBG("str1---index:%d---(%s)\n",i,strVector[i].c_str());
            }

            source=":1,aaaaa,2,bbbbb,,,3,cccccc\r";
            strVector=ComUtils::cutString(source, ":", "\r", ",");
            for(int i=0;i<strVector.size();i++)
            {
                TRACE_DBG("str2---index:%d---(%s)\n",i,strVector[i].c_str());
            }
		}  
        else if ("7"==inputStr)
        {
            std::string source("  00001234");
            std::string result=ComUtils::trimBeginWith(source, "0 ");
            TRACE_RED("result=%s\n",result.c_str());
        }
        else if ("8"==inputStr)
        {
            std::string source("/root1//root2///root3/");
            TRACE_YELLOW("split string notwith Flag \"/\":(%s)\n",source.c_str());
            std::vector<std::string> strVector=ComUtils::splitString(source,"/");
            for(int i=0;i<strVector.size();i++)
            {
                TRACE_DBG("split[%d]---(%s)\n",i,strVector[i].c_str());
            }
            TRACE_YELLOW("split string with Flag \"/\":(%s)\n",source.c_str());
            strVector=ComUtils::splitString(source,"/",true);
            for(int i=0;i<strVector.size();i++)
            {
                TRACE_DBG("split[%d]---(%s)\n",i,strVector[i].c_str());
            }
            source = "1,,,4";
            TRACE_YELLOW("split string with Flag \",\":(%s)\n",source.c_str());
            strVector=ComUtils::splitString(source,",",false);
            for(int i=0;i<strVector.size();i++)
            {
                TRACE_DBG("split[%d]---(%s)\n",i,strVector[i].c_str());
            }
        }
        else if ("9"==inputStr)
        {
            std::string source = "----''''----";
            std::string result = ComUtils::replaceString(source,"'","\\'");
            TRACE_YELLOW("source:%s\nresult:%s\n",source.c_str(),result.c_str());

        }
        else if ("10"==inputStr)
        {
            std::string pidName;
            TRACE_YELLOW("Input pid name:");
            std::cin >> pidName;
            std::cout << pidName<<std::endl;
            std::vector<int> pidList = ComUtils::getPidsByName(pidName.c_str());
            if (pidList.size()==0) 
            {
                TRACE_RED("Can't find process:%s\n",pidName.c_str());
            }
            else
            {
                for (int i=0; i<pidList.size(); i++) 
                {
                    TRACE_YELLOW("pid:%d, Name:%s\n",pidList[i],pidName.c_str());
                }
            }
        }
		inputStr.clear();
	}
}